﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace MetadataBasedConfigArch.Web.Admin
{
    public partial class ManageExtendProperties : System.Web.UI.Page
    {
        #region Page Load

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindData();
            }
        }

        #endregion

        #region Properties

        protected string EditMode
        {
            get
            {
                return (string)(ViewState["EditMode"] ?? string.Empty);
            }
            set
            {
                ViewState["EditMode"] = value;
            }
        }

        protected long ConfigInfoID
        {
            get
            {
                return (long)(ViewState["ConfigInfoID"]??0);
            }
            set
            {
                ViewState["ConfigInfoID"] = value;
            }
        }

        #endregion

        #region Bind Data

        protected void BindData()
        {
            using (Model.MDAEntities entities = EntityUtil.LoadEntities())
            {
                var allConfigInfos = (from ci in entities.ConfigMetaConfigInfoes.Include("ConfigMeta1.ConfigType1")
                              where ci.ConfigMeta1.ConfigType1.Name == "Table"
                                    && ci.TenantID == Util.TenantID
                              select new { TableName=ci.ConfigMeta1.Content,
                                           FieldNick = ci.Name,
                                           FieldName = ci.Content,
                                           ExtendPropertyID = ci.ID,
                                           TenantID= ci.TenantID}
                                  ).ToList();
                ListGridView.DataSource = allConfigInfos;
                ListGridView.DataBind();
            }
        }


        protected void Edit()
        {
            if (ConfigInfoID > 0)
            {
                using (Model.MDAEntities entities = EntityUtil.LoadEntities())
                {
                    var configInfo = (from ci in entities.ConfigMetaConfigInfoes.Include("ConfigMeta1.ConfigType1")
                                          where ci.ConfigMeta1.ConfigType1.Name == "Table"
                                                && ci.TenantID == Util.TenantID
                                          select ci
                                  ).FirstOrDefault();
                    if (configInfo != null)
                    {
                        TableNameTextBox.Text = configInfo.ConfigMeta1.Content;
                        FieldNickTextBox.Text = configInfo.Name;
                        FieldNameTextBox.Text = configInfo.Content;
                                                
                        var typeProperty = (from p in configInfo.ConfigMetaConfigInfoProperties where p.TenantID == Util.TenantID
                                                && p.Key == "DataType" select p).FirstOrDefault();
                        FieldTypeTextBox.Text = typeProperty == null ? String.Empty : typeProperty.Value;
                    }


                    TableNameTextBox.Enabled = false ;
                    TableNameTextBox.ReadOnly = true ;
                }
            }
        }

        #endregion

        #region Button Events

        protected void AddLinkButton_Click(object sender, EventArgs e)
        {
            ConfigInfoID = 0;
            if (String.IsNullOrEmpty(EditMode))
            {
                EditPanel.Visible = true;
                ShowPanel.Visible = false; 
                AddLinkButton.Text = "Go Back";
                EditMode = "Add";
                TableNameTextBox.Enabled = true;
                TableNameTextBox.ReadOnly = false;
            }
            else
            {
                EditPanel.Visible = false;
                ShowPanel.Visible = true;
                AddLinkButton.Text = "Manage Extend Properties";
                EditMode = String.Empty;
            }
        }

        protected void SaveButton_Click(object sender, EventArgs e)
        {
            //if (ConfigInfoID > 0)
            {
                using (Model.MDAEntities entities = EntityUtil.LoadEntities())
                {
                    if (EditMode == "Edit" && ConfigInfoID > 0)
                    { 
                        var configInfo = (from ci in entities.ConfigMetaConfigInfoes.Include("ConfigMeta1.ConfigType1")
                                          where ci.ConfigMeta1.ConfigType1.Name == "Table"
                                                && ci.TenantID == Util.TenantID
                                          select ci
                                  ).FirstOrDefault();
                        if (configInfo != null)
                        {
                            configInfo.Name = FieldNickTextBox.Text;
                            configInfo.Content = FieldNameTextBox.Text;
                            var typeProperty = (from p in configInfo.ConfigMetaConfigInfoProperties
                                                where p.TenantID == Util.TenantID
                                                    && p.Key == "DataType"
                                                select p).FirstOrDefault();
                            if (typeProperty != null)
                            {
                                typeProperty.Value = FieldTypeTextBox.Text;
                            }
                            entities.SaveChanges();
                        }
                    }
                    else if (EditMode == "Add")
                    {
                        var configMeta = (from cm in entities.ConfigMetas
                                          where cm.Content == TableNameTextBox.Text
                                              && cm.ConfigType1.Name == "Table"
                                          select cm).FirstOrDefault();

                        Model.ConfigMetaConfigInfo configInfo = new Model.ConfigMetaConfigInfo();
                        configInfo.Name = FieldNickTextBox.Text;
                        configInfo.Content = FieldNameTextBox.Text; 
                        configInfo.ConfigMeta = configMeta.ID;
                        entities.AddToConfigMetaConfigInfoes(configInfo); 

                        Model.ConfigMetaConfigInfoProperty configInfoProperty = new Model.ConfigMetaConfigInfoProperty(); 
                        configInfoProperty.Key = "DataType";
                        configInfoProperty.Value = FieldTypeTextBox.Text;
                        configInfoProperty.ValidatorType = typeof(MetadataBasedConfigArch.Framework.Validator.DataTypePropertyValidator).AssemblyQualifiedName;
                        configInfoProperty.ConfigInfo = configInfo.ID;
                        entities.AddToConfigMetaConfigInfoProperties(configInfoProperty);

                        entities.SaveChanges();
                        
                    }
                }
            }
            EditPanel.Visible = false;
            ShowPanel.Visible = true;
            EditMode = string.Empty;
            AddLinkButton.Text = "Manage Extend Properties";
            BindData();
        }

        #endregion

        #region List Grid View Event

        protected void ListGridView_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            switch (e.CommandName)
            {
                case "VEdit":
                    {
                        EditPanel.Visible = true;
                        ShowPanel.Visible = false;
                        EditMode = "Edit";
                        AddLinkButton.Text = "Go Back";
                        ConfigInfoID = Convert.ToInt64(e.CommandArgument==null ? "0" : e.CommandArgument);
                        Edit();
                        break;
                    }
                default:
                    break;
            }
        }

        protected void ListGridView_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                var extendPropertyID = (long)(DataBinder.GetPropertyValue(e.Row.DataItem, "ExtendPropertyID") ?? 0);
                Button viewButton = (e.Row.Cells[e.Row.Cells.Count - 1].FindControl("EditButton")) as Button;
                viewButton.CommandArgument = extendPropertyID.ToString();
            }
        }

        #endregion


    }
}